﻿using System.Text.RegularExpressions;
namespace ProxyViewModel.Attributes {
	public class EmailValidatorAttribute : ValidatorBaseAttribute {
		public static Regex EmailRegex = new Regex(@"^([a-zA-Z0-9\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([a-zA-Z0-9\-\.]+\.)+)([a-zA-Z]{2,4}))$");

		private string _errorMessage = "{0} is not a valid email";

		public string ErrorMessage {
			get { return _errorMessage; }
			set { _errorMessage = value; }
		}

		public override ValidationResult Validate(object input) {
			ValidationResult result = new ValidationResult();

			if (!(input is string)) {
				result.IsValid = false;
				result.ValidationError = string.Format("{0} must be of string type", PropertyName);
				return result;
			}

			result.IsValid = EmailRegex.IsMatch((string)input);
			result.ValidationError = result.IsValid ? string.Empty : string.Format(@"""{0}"" is not a valid email", (string)input);
			return result;
		}
	}
}
